package com.yuji.aacar.controller.forgetpassword;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.ImageView;

import com.yuji.aacar.R;
import com.yuji.aacar.base.BaseActivity;
import com.yuji.aacar.base.EventConst;
import com.yuji.aacar.base.UserCenter;
import com.yuji.aacar.bean.response.LoginResponse;
import com.yuji.aacar.bean.response.ResetPasswordResponse;
import com.yuji.aacar.controller.main.TabHomeActivity;
import com.yuji.aacar.core.cores.event.CoreEventBusMessage;
import com.yuji.aacar.core.cores.event.CoreEventCenter;
import com.yuji.aacar.core.cores.services.CoreServiceResponse;
import com.yuji.aacar.core.mvvm.appbase.AppRoute;
import com.yuji.aacar.service.BaseServiceMediator;
import com.yuji.aacar.service.BaseServiceParams;
import com.yuji.aacar.tools.Tools;
import com.yuji.aacar.widgets.autolayout.AutoLinearLayout;
import com.yuji.aacar.widgets.autolayout.AutoRelativeLayout;
import com.yuji.aacar.widgets.edittext.CheckEditText;
import com.yuji.aacar.widgets.edittext.ChooseCancelEditText;
import com.yuji.aacar.widgets.textview.PingFangTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ResetPasswordActivity extends BaseActivity {


    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.back_image)
    AutoRelativeLayout backImage;
    @BindView(R.id.title_content)
    PingFangTextView titleContent;
    @BindView(R.id.back_login)
    PingFangTextView backLogin;
    @BindView(R.id.title_message_iv)
    ImageView titleMessageIv;
    @BindView(R.id.title_message_rl)
    AutoRelativeLayout titleMessageRl;
    @BindView(R.id.title_rl)
    AutoRelativeLayout titleRl;
    @BindView(R.id.reset_password_et)
    ChooseCancelEditText resetPasswordEt;
    @BindView(R.id.reset_password_confirm_et)
    ChooseCancelEditText resetPasswordConfirmEt;
    @BindView(R.id.reset_password_verify_code_et)
    CheckEditText resetPasswordVerifyCodeEt;
    @BindView(R.id.reset_password_verify_code_btn)
    AutoLinearLayout resetPasswordVerifyCodeBtn;
    @BindView(R.id.reset_password_next_btn)
    Button resetPasswordNextBtn;
    @BindView(R.id.reset_code)
    PingFangTextView resetCode;
    private ResetPasswordActivityVM resetPasswordActivityVM;

    private String phone;
    private String code;

    private String mima;
    private String confirmMima;
    private String confirmCode;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_reset_password);
        ButterKnife.bind(this);
        initTitle(true, "重置密码", null);
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        phone = intent.getStringExtra("phone");
        code = intent.getStringExtra("code");
        resetCode.setText(code);

    }

    @Override
    public void alreadyBindBaseViewModel() {
        super.alreadyBindBaseViewModel();
        //        requestParams =
    }

    @Override
    public void handleMessage(CoreEventBusMessage message) {
        super.handleMessage(message);
    }


    @OnClick(R.id.reset_password_next_btn)
    public void onClick() {
        if (isLegal()){
            requestParams = BaseServiceParams.resetpassword(phone, mima, confirmMima, confirmCode);
            doTask(BaseServiceMediator.RESET_PASSWORD);
        }
    }

    @Override
    public void requestSuccess(CoreServiceResponse response) {
        super.requestSuccess(response);
        if (response.getRequestCode() == BaseServiceMediator.RESET_PASSWORD_CODE) {
            ResetPasswordResponse registerResponse = (ResetPasswordResponse) response.getResponse();
            if (registerResponse.isSuccess()) {
                requestParams = BaseServiceParams.login(phone, mima);
                doTask(BaseServiceMediator.LOGIN);
            }
        } else if (response.getRequestCode() == BaseServiceMediator.LOGIN_CODE) {
            UserCenter.saveCurrentUser((LoginResponse) response.getResponse());
            AppRoute.getInstance().nextController(this, TabHomeActivity.class, AppRoute.WITHOUT_RESULTCODE);
            CoreEventCenter.postMessage(EventConst.REGISTER, "注册成功");
            this.finish();
        }
    }

    @Override
    public void requestFailed(CoreServiceResponse response) {
        super.requestFailed(response);
    }


    private boolean isLegal() {
        mima = resetPasswordEt.getText().toString();
        confirmMima = resetPasswordConfirmEt.getText().toString();
        confirmCode = resetPasswordVerifyCodeEt.getText().toString();
        if (!Tools.isPhoneNumberValid(phone)) {
            showToast("请输入正确的手机号！");
            return false;
        }
        if (!Tools.isAccountStandard(mima)) {
            showToast("密码为6~16位数字或英文！");
            return false;
        } else if (!mima.contentEquals(confirmMima)) {
            showToast("密码不一致！");
            return false;
        } else if (TextUtils.isEmpty(confirmCode)) {
            showToast("请输入右侧图片验证码");
            return false;
        } else if (!confirmCode.contentEquals(code)) {
            showToast("图片验证码输入不一致， 大小写区分！");
            return false;
        }
        return true;
    }
}
